<template>
  <div class="swiperList">
    <car-carousel :carouselList="dataList.filter(item => item.state).map(item => item.pictureUrl)"/>
    <el-card class="cardBox">
      <el-form class="s_form" :inline="true">
        <div class="left">
          <el-form-item>
            <div class="cardName">轮播图列表</div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="swiperAdd">新增轮播图</el-button>
          </el-form-item>
        </div>
        <div class="right">
          <el-form-item>
            <el-input size="small" v-model="dataForm.remarks" placeholder="请输入关键字" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="getDataList">查询</el-button>
          </el-form-item>
        </div>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border :height="tabHeight">
        <el-table-column min-width="200" label="轮播图缩略图" show-overflow-tooltip header-align="center" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.pictureUrl" class="avatar">
          </template>
        </el-table-column>
        <el-table-column prop="sort" min-width="100" label="排序" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column prop="remarks" min-width="100" label="备注" show-overflow-tooltip header-align="center" align="center"></el-table-column>
        <el-table-column label="是否显示" header-align="center" align="center" width="100">
          <template slot-scope="scopes">
            <el-switch v-model="scopes.row.state" @change="stateChange(scopes.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="swiperEdit(scope.row)">编辑轮播图</el-button>
            <el-button type="text" size="small" @click="swiperDelete(scope.row)">删除轮播图</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page.sync="curPage" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total"
        layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
    </el-card>
    <swiper-dialog ref="swiperDialog" @refresh="getDataList"/>
  </div>
</template>

<script>
import swiperDialog from './swiperComponent/swiperDialog.vue'
import carCarousel from './swiperComponent/carCarousel.vue'
export default {
  components: { swiperDialog, carCarousel },
  data () {
    return {
      dataListLoading: false,
      dataForm: {
        remarks: ''
      },
      dataList: [],
      tabHeight: '600',
      // 分页
      curPage: 1,
      total: 0,
      limit: 10
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.tabHeight = this.$getClientHeight() - 600 + 'px'
    })
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 新增轮播图
    swiperAdd () {
      this.$refs.swiperDialog.init()
    },
    // 编辑轮播图
    swiperEdit (row) {
      this.$refs.swiperDialog.init(row)
    },
    // 状态改变
    stateChange (row) {
      this.$http.put(`/carlunbo/` + row.id + `/` + (row.state ? 1 : 0), {}).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.$message.success(res.msg)
      }).catch(() => {
      })
    },
    // 删除轮播图
    swiperDelete (row) {
      this.$confirm('您确认要删除' + '该轮播图' + '吗？', '确认删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete('/carlunbo', { data: [row.id] }).then(({ data: res }) => {
          let { code, msg } = res
          if (code === 0) {
            this.$message.success('操作成功!')
            this.getDataList()
          }
          if (code !== 0) {
            this.$message.error(msg)
          }
        })
      }).catch(() => {
      })
    },
    // 获取数据
    getDataList () {
      this.dataListLoading = true
      this.$http.get('/carlunbo/list', {
        params: {
          limit: this.limit,
          page: this.curPage,
          remarks: this.dataForm.remarks
        }
      }).then(({ data: res }) => {
        let { code, msg, page } = res
        console.log(code, msg, page)
        this.dataList = []
        this.total = 0
        if (code === 0 && page) {
          let { list, total } = page
          this.dataList = list && list.length ? list : []
          this.dataList.map(item => {
            item.state = item.state === 1
          })
          this.total = total
          this.dataListLoading = false
        }
        if (code !== 0) {
          this.$message.error(msg)
        }
        this.dataListLoading = false
      })
    },
    // 切换每页数量
    pageSizeChangeHandle (size) {
      console.log(size)
      this.limit = size
      this.curPage = 1
      this.getDataList()
    },
    // 切换页
    pageCurrentChangeHandle (curPage) {
      this.curPage = curPage
      this.getDataList()
    }
  }
}
</script>

<style lang="scss">
.swiperList {
  padding: 20px;
  .cardBox {
    width: 100%;
    min-width: 1100px;
    background-color: rgba(255, 255, 255, 1);
    margin-top: 15px;
    .s_form {
      display: flex;
      justify-content: space-between;
      .cardName {
        font-weight: bold;
        font-size: 18px;
      }
      .s_input {
        width: 100px;
      }
    }
  }
  .avatar{
    width: 200px;
    height: 100px;
  }
}
</style>
